<?php

/* 
 * Manager/FileManager.php
 */
namespace RHX\Model\Manager;

use Zend\Db\Adapter\Adapter;

class FileManager{
    protected $adapter = null;
    
    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    
    const EMPLOYE_CONTRAT_FILE =  "contrat_file";
    const EMPLOYE_AVENANT_FILE =  "avenant_file";
    const EMPLOYE_RIB_FILE     =  "rib"; 
    const EMPLOYE_SEJOUR_NUM_FILE   =  "sejour_num_file";
    const EMPLOYE_CV_FILE      =  "cv_path";
    
    const SITE_DOC_FILE      =  "site_doc";
    
    
    /* Drapeau indiquant quel telechargement est concerné
     * Ce drapeau designe le nom du champ dans la Table Concernée, 
     */
    protected $flag = null;
    
    /* Dossier d' upload
     */
    protected $configDirectory =  array (
        'employe'=> array(
            'photo'             => 'public/upload/photo/',
            'rib'               => 'public/upload/rib/',
            'cv'                => 'public/upload/cv/',
            'titre_de_sejour'   => 'public/upload/sejournum/',
            'contrat'           => 'public/upload/contrat/',
            'avenant'           => 'public/upload/avenant/'            
            ),
        'site'=> array(
            'photo' => 'public/upload/site/photo/',
            'rib'   => 'public/upload/site/rib/',
            'doc'   => 'public/upload/site/doc/'
            ),
        'tiers'=> array(
            'photo' => 'public/upload/tiers/photo/',
            'rib'   => 'public/upload/tiers/rib/'
            ) 
        );
    
    public function __construct(Adapter $adapter){
	$this->adapter=$adapter;        
    }
    
    /**
     * Adapteur de base de données
     * 
     * @return Adapter
     */
    public function getAdapter(){
	return $this->adapter;
    }
    
    /* Setter flag - specifie le flag pour les fuftur utilisatin interne ou externe de la classe
     * @param $flag 
     */
    public function setFlag($flag){
        $this->flag = $flag;
    }
    
    /* Getter flag
     */
    public function getFlag(){
        return $this->flag;
    }
    
    /* recupérer extensions pouvant être telecharger
     */
    
    public function getExtensions(){        
        switch($this->flag){
            case self:: EMPLOYE_CONTRAT_FILE:                 
            case self:: EMPLOYE_AVENANT_FILE:
            case self:: EMPLOYE_RIB_FILE:
            case self:: EMPLOYE_SEJOUR_NUM_FILE:
            case self:: EMPLOYE_CV_FILE:
            case self:: SITE_DOC_FILE:
                $exts = array('jpg', 'png', 'gif', 'pdf');
            break;
        
            default:
                $exts = array();    
        }
        return isset($exts) ? $exts : array();
    }
   
    /* Recupérer le dossier de stockage de fichier
     */
    public function getUploadDirectory(){
        switch($this->flag){
            case self::EMPLOYE_CONTRAT_FILE:
                 $dir = $this->configDirectory['employe']['contrat'];
                break;
            
            case self::EMPLOYE_AVENANT_FILE:
                 $dir = $this->configDirectory['employe']['avenant'];
                break;
            
            case self:: EMPLOYE_RIB_FILE:
                $dir = $this->configDirectory['employe']['rib'];
                break;
            
            case self:: EMPLOYE_SEJOUR_NUM_FILE:
                $dir = $this->configDirectory['employe']['titre_de_sejour'];
                break;
            
            case self:: EMPLOYE_CV_FILE:
                $dir = $this->configDirectory['employe']['cv'];
                break;
            case self:: SITE_DOC_FILE:
                $dir = $this->configDirectory['site']['doc'];
                break;
            default:
                $dir = '';
                
        }
        return isset($dir) ? $dir : '';
    }
    
    /* Format le nom du fichier à traiter
     * @param array $data 
     */
    public function formatName($data){  
        $format = array(
            self:: EMPLOYE_CONTRAT_FILE => 'contrat',
            self:: EMPLOYE_AVENANT_FILE => 'avenant',
            self:: EMPLOYE_RIB_FILE     => 'rib',
            self:: EMPLOYE_SEJOUR_NUM_FILE => 'sejournum',
            self:: EMPLOYE_CV_FILE      => 'cv',
            self:: EMPLOYE_CV_FILE      => 'document'
        );
        
        
        $prefix = isset($format[$this->flag]) ? $format[$this->flag]: '';        
        $glue   = '-'   ;        
        $formated = implode($glue, $data);
        
        return $prefix. $glue. $formated;        
    }
    
    /* Recupérer l'action pour la sauvegarde des noms de fichiers, correspondant au flag de l'objet instancié
     * 
     * @return string
     */
    public function getActionSaving(){        
        $action = array(
            self::EMPLOYE_CONTRAT_FILE      => array('controller' => 'employe', 'action' => 'saveEmployeStringData'), 
            self::EMPLOYE_RIB_FILE          => array('controller' => 'employe', 'action' => 'saveEmployeStringData'), 
            self::EMPLOYE_SEJOUR_NUM_FILE   => array('controller' => 'employe', 'action' => 'saveEmployeStringData'),
            self::EMPLOYE_CV_FILE           => array('controller' => 'employe', 'action' => 'saveEmployeStringData'),
            self::EMPLOYE_AVENANT_FILE      => array('controller' => 'employe', 'action' => 'saveAvenantStringData'),
            self::SITE_DOC_FILE             => array('controller' => 'site'   , 'action' => 'saveDocument')            
        );
        
        $savingAction = isset($action[$this->flag] ) ? $action[$this->flag]  : array('controller' => '', 'action' => '' );
        
        return $savingAction;                
    }
    
    /* renommer un fichier dependamment du flag actuel de l'instance
     * @param array $dataFormatName : les données utilisé pour le format de nommage du fichier     
     * @param string $fileInDisk : nom du fichier actuelle
     * @return string $ret  Nouveau nom du fichier
     */
    
    public function renameFile($dataFormat, $fileInDisk){       
        
        $dirPublic = $this->getUploadDirectory();        
        $dir = str_replace('public/', '', $dirPublic) ;
        
        $newFile = $this->formatName($dataFormat['format']). '.' . $dataFormat['extension'];        
        if (@rename($dirPublic . $fileInDisk, $dirPublic .$newFile )) {
            $ret = $newFile; 
        } 
        elseif (@rename($dir . $fileInDisk, $dir . $newFile)) {
            $ret = $newFile;
        }
        
        return isset($ret) ? $ret : '';        
    }
    
    /* Fournit le tire de la vue de telechargement du fichier
     * 
     * @return string
     */
    public function getTitleView(){
        $titles = array(
            FileManager::EMPLOYE_CONTRAT_FILE   => "Téléchargement de la pièce jointe d'un contrat",
            FileManager::EMPLOYE_AVENANT_FILE   => "Téléchargement de la pièce jointe d'un avenant",
            FileManager::EMPLOYE_CV_FILE        => "Téléchargement du CV",
            FileManager::EMPLOYE_RIB_FILE       => "Téléchargement du RIB",
            FileManager::EMPLOYE_SEJOUR_NUM_FILE=> "Téléchargement du N° de titre de séjour",
            FileManager::SITE_DOC_FILE          => "Téléchargement de document pour le site"               
        );
        $sTitle = isset($titles[$this->getFlag()]) ? $titles[$this->getFlag()] : '';
        
        return $sTitle;
    }
    
   
}

